package com.shujia.jinjie;

import org.bouncycastle.crypto.modes.EAXBlockCipher;
import redis.clients.jedis.Jedis;

import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/*
    Redis充当短信验证码，设置过期时间

    1、创建一个User类，作为读取mysql用户数据的实体类 getAllUsers()
    2、创建一些工具类：
        CodeTool 主要是生成验证码，发送验证码，将验证码放到redis中
        MySqlTool 主要是获取与数据库的连接对象
        redisTool 主要是获取与单个redis服务的连接对象



 */
public class AnliDemo1 {
    private static final List<User> ALL_USERS = getAllUsers();
    private static final Jedis REDIS_CONN = RedisTool.getRedisConnection();

    public static void main(String[] args) {
        init();
    }

    public static void init() {
        System.out.println("=================== 欢迎登录张成阳婚姻介绍所 ====================");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要做的操作：(1.登录 2.注册)");
        if (sc.hasNextInt()) {
            switch (sc.nextInt()) {
                case 1:
                    System.out.println();
                    System.out.println();
                    login();
                    break;
                case 2:
                    System.out.println();
                    System.out.println();
                    register();
                    break;
                default:
                    System.out.println();
                    System.out.println();
                    System.out.println("没有该选项！");
                    break;
            }
        } else {
            System.out.println("您输入的类型有误！");
        }

    }

    public static void login() {
        System.out.println("请选择登录的方式：【1.密码登录 2.手机验证码登录】");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        if(choice==1){
            System.out.println("欢迎登录！");
            System.out.println("请输入您的用户名：");
            String name = sc.next();

            User u = null;
            for (User user : ALL_USERS) {
                if (name.equals(user.getName())) {
                    u = user;
                    break;
                }
            }

            // 从map集合中判断用户是否存在
            if (u!=null) {
                System.out.println("请输入用户的密码：");
                String pwd = sc.next();
                if (pwd.equals(u.getPassword())) {
                    System.out.println("登录成功！！");
                } else {
                    System.out.println("登录失败！密码不正确！");
                }
            } else {
                System.out.println("该用户还未注册！");
            }
        }else {
            System.out.println("欢迎登录！");
            System.out.println("请输入手机号：");
            String phoneNum = sc.next();

            User u = null;
            for (User user : ALL_USERS) {
                if (phoneNum.equals(user.getPhoneNumber())) {
                    u = user;
                    break;
                }
            }
            // 从map集合中判断用户是否存在
            if (u!=null) {
                System.out.println("正在发送验证码，请稍后~");
                //调用api发送验证码
                String status = CodeTool.sendCode(u);
                if("OK".equals(status)){
                    while (true){
                        System.out.println("请输入收到的验证码：");
                        String yzm = sc.next();
                        String redisRes = REDIS_CONN.get(u.getName());
                        if(redisRes!=null && redisRes.equals(yzm)){
                            System.out.println("登录成功！！");
                            break;
                        }else {
                            System.out.println("验证码输入错误！！！");
                        }
                    }
                }
            } else {
                System.out.println("该手机号还未注册！");
            }
        }
    }

    public static void register() {
        BufferedWriter bw = null;
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("欢迎注册！");
            System.out.println("请设置您的用户名：");
            String name = sc.next();
            User u = null;
            for (User user : ALL_USERS) {
                if (name.equals(user.getName())) {
                    u = user;
                    break;
                }
            }

            // 从map集合中判断用户是否存在
            if (u!=null) {
                System.out.println("该用户名已被使用！！");
            } else {
                System.out.println("请设置您的账户密码：");
                String pwd = sc.next();
                System.out.println("请设置您的账户邮箱：");
                String email = sc.next();
                System.out.println("请设置您的账户手机号：");
                String phoneNum = sc.next();
                System.out.println("请设置您的账户地址：");
                String address = sc.next();
                //将用户名和密码写入到文件中【追加写】
                bw = new BufferedWriter(new FileWriter("src/shujia/day14/users.txt", true));
                bw.write(name + "|" + pwd+"|"+email+"|"+phoneNum+"|"+address);
                bw.newLine();
                bw.flush();

                System.out.println("用户注册成功！！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static List<User> getAllUsers() {

        ArrayList<User> userList = new ArrayList<>();

        try {
            //获取与数据库的连接对象
            Connection conn = MySqlTool.getConnection();
            //查询所有的用户数据
            Statement statement = conn.createStatement();

            ResultSet resultSet = statement.executeQuery("select name,password,email,phoneNumber from users");

            while (resultSet.next()){
                String name = resultSet.getString("name");
                String password = resultSet.getString("password");
                String email = resultSet.getString("email");
                String phoneNumber = resultSet.getString("phoneNumber");

                User user = new User(name, password, email, phoneNumber);
                userList.add(user);

            }

        }catch (Exception e){
            e.printStackTrace();
        }


        return userList;

    }
}
